package com.ruoyi.project.lely.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.R;
import com.ruoyi.common.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.lely.constant.CommonConstant;
import com.ruoyi.project.lely.bean.entity.LyAddress;
import com.ruoyi.project.lely.bean.entity.LyCollect;
import com.ruoyi.project.lely.bean.entity.LyCoupon;
import com.ruoyi.project.lely.bean.entity.LyGoods;
import com.ruoyi.project.lely.bean.entity.LyUser;
import com.ruoyi.project.lely.mapper.LyAddressMapper;
import com.ruoyi.project.lely.mapper.LyCollectMapper;
import com.ruoyi.project.lely.mapper.LyUserMapper;
import com.ruoyi.project.lely.service.LyCouponService;
import com.ruoyi.project.lely.service.LyGoodsService;
import com.ruoyi.project.lely.service.LyOrderService;
import com.ruoyi.project.lely.service.LyUserService;
import com.ruoyi.project.lely.bean.vo.LyGoodsCollectionVO;
import com.ruoyi.project.lely.bean.vo.LyUserCountVO;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Desc: 用户信息
 * @Author LuoYong
 * @Date 2022/1/17 18:14
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class LyUserServiceImpl extends ServiceImpl<LyUserMapper, LyUser> implements LyUserService {

    @Autowired
    private LyOrderService orderService;
    @Autowired
    private LyCouponService couponService;
    @Autowired
    private LyGoodsService goodsService;
    @Autowired
    private LyCollectMapper collectMapper;
    @Autowired
    private LyAddressMapper addressMapper;


    /**
     * 获取我的订单统计相关
     */
    @Override
    @DataSource(value = DataSourceType.SLAVE)
    public R getCount() {
        Long userId = SecurityUtils.getUserId();
        LyUser user = this.getUser(userId,null);
        // 查询用户的优惠券数量
        int total = couponService.count(
                new LambdaQueryWrapper<LyCoupon>()
                        .eq(LyCoupon::getType, 0)
                        .eq(LyCoupon::getStatus, 0)
                        .eq(LyCoupon::getDeleted, CommonConstant.DELETED)
        );
        // 查询用户订单统计
        LyUserCountVO vo = orderService.getOrderAccountById(userId);
        vo.setCouponCount(total);
        if(ObjectUtil.isNotNull(user)){
            vo.setMemberPoints(user.getFraction());
            vo.setType(user.getUserLevel());
        }
        return R.ok(vo);
    }

    @DataSource(value = DataSourceType.SLAVE)
    public LyUser getUser(Long userId, String openId){
        if(ObjectUtil.isNotNull(userId)){
            userId = SecurityUtils.getUserId();
        }
        return this.getOne(
                new LambdaQueryWrapper<LyUser>()
                        .eq(LyUser::getId, userId)
                        .eq(StrUtil.isNotEmpty(openId),LyUser::getWeixinOpenid, openId)
        );
    }

    /**
     * 获取我的收藏
     */
    @Override
    public R getCollect(Page<LyCollect> page) {
        Long userId = SecurityUtils.getUserId();
        Page pageList = collectMapper.selectPage(
                page,
                new LambdaQueryWrapper<LyCollect>()
                        .eq(LyCollect::getUserId, userId)
                        .eq(LyCollect::getType, 0)
                        .eq(LyCollect::getDeleted, CommonConstant.DELETED)
        );
        List<LyGoodsCollectionVO> vos = new ArrayList<>();
        if (pageList != null && CollUtil.isNotEmpty(pageList.getRecords())) {
            List<LyCollect> records = pageList.getRecords();
            records.stream().forEach(n -> {
                LyGoods goods = goodsService.getById(n.getValueId());
                if (goods != null) {
                    LyGoodsCollectionVO vo = new LyGoodsCollectionVO();
                    BeanUtils.copyProperties(n, vo);
                    vos.add(vo);
                }
            });
            pageList.setRecords(vos);
        }
        return R.ok(pageList);
    }

    /**
     * 获取我的地址
     */
    @Override
    public R getAddress(Page<LyAddress> page) {
        Long userId = SecurityUtils.getUserId();
        Page<LyAddress> pageList = addressMapper.selectPage(
                page,
                new LambdaQueryWrapper<LyAddress>()
                        .eq(LyAddress::getUserId, userId)
                        .eq(LyAddress::getDeleted, CommonConstant.DELETED)
                        .orderByDesc(LyAddress::getIsDefault)
        );
        return R.ok(pageList);
    }
}
